/*
 * Copyright 2013 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.view.velocity.tools;

import java.util.Calendar;
import java.util.Date;

import org.apache.velocity.tools.config.DefaultKey;
import org.apache.velocity.tools.generic.SafeConfig;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Scope;

import com.fujitsu.nanjing.tieba.ibator.CoreBoard;
import com.fujitsu.nanjing.tieba.ibator.CoreGroup;
import com.fujitsu.nanjing.tieba.ibator.CoreUser;
import com.fujitsu.nanjing.tieba.service.BoardService;
import com.fujitsu.nanjing.tieba.service.GroupService;
import com.fujitsu.nanjing.tieba.service.ShortMessageService;
import com.fujitsu.nanjing.tieba.service.UserService;

/**
 * AuthTool.java
 * 
 * @author Administrator
 */
@Configurable("authTool")
@DefaultKey("auth")
@Scope
@DependsOn
public class AuthTool extends SafeConfig {

	private static final String 新人期 = "newMemberDay";
	private UserService userService;
	private GroupService groupService;
	private BoardService boardService;
	private ShortMessageService shortMessageService;
	public String hasAuth(String userId, String authorId, String boardId) {
		CoreUser user = userService.findById(userId);
		String result = "false";
		if (user != null) {
			if (userId.equals(authorId)) {
				result = "true";
			} else {
				result = isAdmin(userId, boardId);
			}
		}
		return result;
	}
	public String isAdmin(String userId, String boardId) {
		CoreUser user = userService.findById(userId);
		if (user == null)
			return "false";
		String result = "false";
		CoreGroup group = groupService.findById(user.getGroupid());
		if (group.getGrouptype().equals("S")) {
			// 系统
			if (!group.getGroupid().equals("G")) {
				// 非游客
				if (group.getGroupid().equals("A")) {
					// 管理员
					result = "true";
				} else {
					CoreBoard board = boardService.findById(boardId);
					String[] moders = board.getModerator().split(",");
					for (String moder : moders) {
						if (moder.equals(userId)) {
							// 版主
							result = "true";
							break;
						}
					}
				}
			}
		}
		return result;

	}
	public String admin(String userId) {
		CoreUser user = userService.findById(userId);
		if (user == null) {
			return "false";
		} else if (user.getGroupid().equals("A")) {
			return "true";
		}
		return "false";
	}
	public String getUserName(String userid) {
		CoreUser user = userService.findById(userid);
		if (user == null) {
			return "未知";
		}
		return user.getNickname();
	}
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public BoardService getBoardService() {
		return boardService;
	}

	public void setBoardService(BoardService boardService) {
		this.boardService = boardService;
	}

	public String getName(String userids) {
		String[] userIds = userids.split(",");
		StringBuilder sb = new StringBuilder();
		for (String userId : userIds) {
			CoreUser user = userService.findById(userId);
			if (user != null) {
				sb.append(user.getNickname() + ",");
			}
		}
		sb.substring(0, sb.length() - 1);
		return sb.toString();

	}
	public String isNewMember(String userid) {
		CoreUser user = userService.findById(userid);
		if (user == null) {
			return "false";
		}
		Date createTime = user.getCreatetime();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(createTime);
		calendar.add(Calendar.DATE,
				Integer.parseInt(new DomainTool().info(新人期)));
		if (calendar.getTime().after(new Date())) {
			return "true";
		}
		return "false";
	}
	public int unReadShortMsgs(String userId) {
		return shortMessageService.countUnReadByUserId(userId);
	}
	public ShortMessageService getShortMessageService() {
		return shortMessageService;
	}
	public void setShortMessageService(ShortMessageService shortMessageService) {
		this.shortMessageService = shortMessageService;
	}
}
